﻿using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Xna.Framework;

namespace TetraEngine
{
    public class GameBoard
    {
        public static readonly int GameBoardRows = 18;
        public static readonly int GameBoardColumns = 10;
        int[,] board = new int[GameBoardRows, GameBoardColumns];

        public GameBoard()
        {
            //for (int i = 7; i < 18; i++)
            //    for (int j = 0; j < 9; j++)
            //        board[i, j] = 1;
        }


        public int this[int row, int column]
        {
            get { return board[row, column]; }
            set { board[row, column] = value; }
        }


        public bool CollidesWithPiece(Piece piece, Point offset)
        {
            if (piece == null) return false;
            BlockGrid grid = piece.GetBlockGrid();
            for (int i = 0; i < grid.Size; i++)
            {
                for (int j = 0; j < grid.Size; j++)
                {
                    if (!IsOnBoard(new Point(j + offset.X, i + offset.Y)))
                        if (grid[i, j] != 0)
                            return true;
                        else
                            continue;
                    if (board[i + offset.Y, j + offset.X] != 0 && 
                        grid[i, j] != 0)
                        return true;
                }
            }
            return false;
        }

        public void PlacePiece(Piece piece, Point offset)
        {
            BlockGrid grid = piece.GetBlockGrid();
            for (int i = 0; i < grid.Size; i++)
            {
                for (int j = 0; j < grid.Size; j++)
                {
                    if (!IsOnBoard(new Point(j + offset.X, i + offset.Y)))
                        continue;
                    if (grid[i, j] > 0)
                        board[i + offset.Y, j + offset.X] = grid[i, j];
                }
            }
        }

        public bool IsOnBoard(Point location)
        {
            return (location.X < GameBoardColumns &&
                location.Y < GameBoardRows &&
                location.X >= 0 && location.Y >= 0);
        }
    }
}
